#include <bits/stdc++.h>

using namespace std;

class Solution {
    public:
        long long maxSum(vector<vector<int>>& grid, vector<int>& limits, int k) {
            priority_queue<int> final;
            int n = grid.size();
            for(int i = 0; i < n; ++i){
                auto & row = grid[i];
                int limit = limits[i];
                priority_queue<int> hh;
                for(auto num: row){
                    hh.push(num);
                }
                while(limit--){
                    int p = hh.top(); hh.pop();
                    final.push(p);
                }
            }
            long long ans = 0;
            while(k--){
                int p = final.top(); final.pop();
                ans += (long long)p;
            }
            return ans;
        }
    };

int main() {
    
    //system("pause");
    return 0;
}
